% fig1 分岔图 (parfor + 函数封装)
clc; clear; close all;
% ——— 参数设置 ———
x0 = 0.463442265;    % 初始 x
y0 = 0.04532285;     % 初始 y
z0 = 0.002136285;    % 初始 z
b  = 30;             % 参数 β
Ntrans = 500;        % 丢弃前 Ntrans 步作为暂态
Nkeep  = 300;        % 记录后 Nkeep 步的 x 值
Nr     = 1200;       % γ 的采样点数
r_list = linspace(2.9,4,Nr);  % γ ∈ [3,4]

% ——— 预分配结果矩阵 ———
R_grid = zeros(Nr, Nkeep);
X_grid = zeros(Nr, Nkeep);

% ——— 并行计算分岔数据 ———
parfor ir = 1:Nr
    r = r_list(ir);
    % 初始化状态
    x = x0; y = y0; z = z0;
    x_prev = x; z_prev = z;
    x_out = zeros(1, Nkeep);

    for k = 1:(Ntrans + Nkeep)
        % 调用封装好的单步迭代函数
        [x_new, y_new, z_new] = Logistic(x, y, z, x_prev, z_prev, r, b);
        % 更新前后状态
        x_prev = x;  z_prev = z;
        x = x_new;   y = y_new;   z = z_new;

        % 记录后 Nkeep 步
        if k > Ntrans
            x_out(k - Ntrans) = x;
        end
    end

    % 写入本行结果
    R_grid(ir, :) = r;
    X_grid(ir, :) = x_out;
end

% ——— 展平并绘图 ———
R_values = reshape(R_grid.', [], 1);
X_values = reshape(X_grid.', [], 1);

figure;
plot(R_values, X_values, '.k', 'MarkerSize', 1);
xlabel('\gamma');
ylabel('x');
box on;
